﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Touch2Like.DAL;
using Touch2Like.Model;
using Touch2Like.SharedLibrary;

namespace Touch2Like.Web
{
    public partial class Touch : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
            Response.Expires = -1500;
            Response.CacheControl = "no-cache";
            Response.Cache.SetNoStore();

            string browserguid = "";
            if (Request.Cookies["BrowserGUID"] != null && Request.Cookies["BrowserGUID"].Value != null)
                browserguid = Request.Cookies["BrowserGUID"].Value;
            else
            {
                browserguid = Guid.NewGuid().ToString();
                HttpCookie browserguidcookie = new HttpCookie("BrowserGUID", browserguid);
                browserguidcookie.Expires = DateTime.Now.AddDays(365);
                Response.SetCookie(browserguidcookie);
            }

            if (Request["taguid"] == null)
                WebUtility.Authentication.SendError(this.Page, 5000, "Invalid request!");
            else
            {

                UserTagDAL tag = new UserTagDAL();
                UserTagInfo tagInfo = tag.GetUserTagByTagUID(Request["taguid"]);


                if (tagInfo != null)
                {
                    ActivityDAL activity = new ActivityDAL();
                    ActivityInfo activityInfo = new ActivityInfo();
                    activityInfo.TagUID = tagInfo.TagUID;
                    activityInfo.TagContent = tagInfo.TagContent;
                    activityInfo.TagType = tagInfo.TagType;
                    activityInfo.TagDate = DateTime.Now;
                    activityInfo.DeviceID = "";
                    activityInfo.IMSINumber = "";
                    activityInfo.PhoneNo = 0;
                    activityInfo.PhoneModel = "";
                    activityInfo.Longitude = "";
                    activityInfo.Latitude = "";
                    activityInfo.BrowserGUID = browserguid;

                    int activityid = activity.AddActivity(activityInfo);

                    switch (tagInfo.TagType)
                    {
                        case "Touch2Rate":
                            Response.Write("<script>document.location.href='/Touch2Rate/Survey.aspx?activityid=" + activityid + "&taguid=" + Request["taguid"] + "';</script>");
                            break;
                        //case "Touch2Play":
                        //    Response.Write("<script>document.location.href='/Touch2Play/Play.aspx?&taguid=" + Request["taguid"] + "';</script>");
                        //    break;
                        default:
                            if (tagInfo != null)
                            {
                                if (string.IsNullOrEmpty(tagInfo.TagContent))
                                    WebUtility.Authentication.SendError(this.Page, 5001, "You have not set up the destination yet");
                                else
                                {
                                    Response.Redirect(tagInfo.TagContent.ToString(), false);
                                    return;
                                }
                            }
                            break;
                    }
                }
            }
        }
    }
}